package com.rongshu.view.home.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.DateUtils;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.home.adapter.DaySignListAdapter;
import com.rongshu.view.home.adapter.DaySignTypePageAdapter;
import com.rongshu.view.home.entity.DaySignListResponse;
import com.rongshu.view.home.presenter.DaySignListPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.utils.DensityUtil;
import com.widget.datapicker.builder.TimePickerBuilder;
import com.widget.datapicker.listener.OnTimeSelectChangeListener;
import com.widget.datapicker.listener.OnTimeSelectListener;
import com.widget.datapicker.view.TimePickerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2019/2/20.
 * description：
 */
public class DaySignListFragment extends BaseFragment<DaySignListPresenter> implements INetView {
    @BindView(R.id.top_option)
    LinearLayout topOption;
    @BindView(R.id.day_sign_list)
    RecyclerView daySignList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshView;
    @BindView(R.id.current_date)
    TextView currentDateTv;
    @BindView(R.id.view_stub_empty)
    ViewStub empty;
    private List<DaySignListResponse.ResponseBean> list;
    private DaySignListAdapter adapter;
    //日期
    private TimePickerView pvTime;
    private int type;
    private String currentDate;
    private int pageIndex = 1;
    private int pageSize = 10;
    private View inflate;
    private RelativeLayout layoutEmpty;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_day_sing_list;
    }

    @Override
    protected void initViews() {
        Bundle bundle = getArguments();
        type = bundle.getInt(Constant.INTENT_TYPE);
        topOption.setVisibility(type == Constant.DAY_SIGN_RONGSHU ? View.VISIBLE : View.GONE);
        if (type == Constant.DAY_SIGN_AUTHOR){
            //我的日签，与顶部留出距离
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) refreshView.getLayoutParams();
            layoutParams.topMargin = DensityUtil.getStateBar(getActivity());
            refreshView.setLayoutParams(layoutParams);
        }
        list = new ArrayList<>();
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        daySignList.setLayoutManager(manager);
        adapter = new DaySignListAdapter(getActivity(), list, type);
        daySignList.setAdapter(adapter);
        if (type == Constant.DAY_SIGN_RONGSHU) {
            //初始化日期
            initTimePicker();
        }
    }

    @Override
    protected void initData() {
        currentDateTv.setText(DateUtils.getToday("yyyy年MM月"));
        currentDate = DateUtils.getToday(DateUtils.YYYY_MM_DD);
        setPresenter(new DaySignListPresenter(getActivity()), this);
        refreshView.setEnableAutoLoadmore(false);
        refreshView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.resetNoMoreData();
                pageIndex = 1;
                presenter.getDaySignList(type, pageIndex, pageSize, currentDate);
            }
        });
        refreshView.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getDaySignList(type, pageIndex, pageSize, currentDate);
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (presenter != null) {
            pageIndex = 1;
            refreshView.autoRefresh();
        }
    }

    @OnClick(R.id.top_option)
    public void onViewClicked() {
        pvTime.show();
    }

    private void initTimePicker() {//Dialog 模式下，在底部弹出
        Calendar startDate = Calendar.getInstance();
        //日签日期从三月份开始
        startDate.set(2019, 3, 0);
        Date curDate = new Date(System.currentTimeMillis());
        Calendar endDate = Calendar.getInstance();
        endDate.setTime(curDate);
        if (startDate.getTimeInMillis() > endDate.getTimeInMillis()) {
            endDate = startDate;
        }
        pvTime = new TimePickerBuilder(getActivity(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                pageIndex = 1;
                refreshView.resetNoMoreData();
                currentDate = DateUtils.formatDate("yyyy-MM", date);
                currentDateTv.setText(DateUtils.formatDate("yyyy年MM月", date));
                presenter.getDaySignList(type, pageIndex, pageSize, currentDate);
            }
        }).setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
            @Override
            public void onTimeSelectChanged(Date date) {
            }
        }).setType(new boolean[]{true, true, false, false, false, false})
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .setSubmitText(getString(R.string.complete))
                .setSubmitColor(getResources().getColor(R.color.color_3cc051))
                .setCancelColor(getResources().getColor(R.color.color_3cc051))
                .setTitleBgColor(ContextCompat.getColor(getActivity(), R.color.white_loading_color))
                .setBgColor(ContextCompat.getColor(getActivity(), R.color.white_loading_color))
                .setTextColorCenter(ContextCompat.getColor(getActivity(), R.color.color_4a4a4a))
                .setTextColorOut(ContextCompat.getColor(getActivity(), R.color.color_666666))
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .setDate(endDate)
                .build();
    }

    @Override
    public void success(Object object, int tag) {
        if (object instanceof DaySignListResponse) {
            DaySignListResponse response = (DaySignListResponse) object;
            if (response == null) {
                return;
            }
            if (pageIndex == 1) {
                list.clear();
            }
            if (refreshView.isLoading()) {
                refreshView.finishLoadmore();
            }
            if (refreshView.isRefreshing()) {
                refreshView.finishRefresh();
            }
            list.addAll(response.getResponse());
            adapter.notifyDataSetChanged();
            if (response.getResponse().size() == 0) {
                refreshView.finishLoadmoreWithNoMoreData();
            }
            if (list.size() == 0) {
                showEmptyLayout();
            } else {
                hideEmptyLayout();
            }
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }

    private void showEmptyLayout() {
        if (inflate == null) {
            inflate = empty.inflate();
            layoutEmpty = inflate.findViewById(R.id.layout_empty);
            ImageView noDataImg = inflate.findViewById(R.id.no_data_img);
            TextView noDataHint = inflate.findViewById(R.id.no_data_hint);
            noDataImg.setImageResource(R.drawable.empty_day_sign);
            noDataHint.setText("暂无日签");
        }
        layoutEmpty.setVisibility(View.VISIBLE);
    }

    private void hideEmptyLayout() {
        if (layoutEmpty != null) {
            layoutEmpty.setVisibility(View.GONE);
        }
    }
}
